package com.atguigu.day5;

import java.util.concurrent.CountDownLatch;

class MyNumber {
    //测试volatile不保证原子性
    volatile int number = 0;

    //本方法没有加synchronized
    public synchronized void add() {

            number++;


    }
}

public class VolatileNotAtomicDemo {
    public static final int NUMBER200 = 200;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(NUMBER200);
        MyNumber myNumber = new MyNumber();
        for (int i = 1; i <= NUMBER200; i++) {
            new Thread(() -> {
                for (int j = 1; j <= NUMBER200; j++) {
                    myNumber.add();
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "\t" + myNumber.number);
    }
}
